package com.cwf.business.preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Created by sam_chan on 2017/5/8.
 * 类功能描述：</br> 使用文件持久化一些用户使用信息
 */
public class PreferencesUtils {

    private final SharedPreferences sp;
    private static PreferencesUtils preferencesUtils;

    /**
     * 文件的名称
     */
    private static final String USER_PREFRENCE_FILE = "User_Preferences";

    /**
     * 用户第一次打开app
     */
    public static final String FRIST_ONE_OPEN_APP = "first_one_open_app";

    /**
     * 启动页到引导页GuideActivity的间隔时间
     */
    public static final String EXTRA_DELAY_TIME = "extra_delay_time";

    /**
     * 用户是否保存登录信息
     */
    public static final String SAVE_USER_INFO = "save_user_info";

    /**
     * 记录上一次所在的城市
     */
    public static final String SAVE_LASTTIME_CITY = "save_lasttime_city";

    /**
     * 用户是否已阅读操作指南，默认第一次登录后会自动显示操作指南
     */
    public static final String GUIDE_STATE_KEY = "GUIDE_STATE";

    /**
     * 保存通知栏状态
     */
    public static final String SETTING_NOTIFICATION_STATUS = "setting_notification_status";

    /**
     * 并且保存用户信息用于手势登录
     */
    public static final String NINEPOINT_USER_INFO = "ninepoint_user_info";

    /**
     * 用户的id
     */
    public static final String USER_ID = "USER_ID";

    /**
     * 保存用户踢线提示
     */
    public static final String USER_KICK_PROMPT = "user_kick_prompt";

    /**
     * 该账户是否第一次登陆
     */
    public static final String USER_NEW_LOGIN = "user_new_login";

    /**
     * 登录成功后保存当前应用版本号
     **/
    public static final String SAVE_LOGIN_VERSION = "save_login_version";

    private PreferencesUtils(Context context) {
        sp = context.getSharedPreferences(USER_PREFRENCE_FILE, 0);
        // PreferencesUtils.context = context;
    }

    public static synchronized void createInstance(Context context) {
        if (preferencesUtils == null) {
            preferencesUtils = new PreferencesUtils(context);
        }
    }

    public static synchronized PreferencesUtils getInstance() {
        return preferencesUtils;
    }

    /**
     * 获取REFRENCE文件中的值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T getPreferences(String key, T defaultValue) {
        if (defaultValue instanceof Integer) {
            return (T) new Integer(sp.getInt(key, (Integer) defaultValue));
        } else if (defaultValue instanceof String) {
            return (T) sp.getString(key, (String) defaultValue);
        } else if (defaultValue instanceof Boolean) {
            return (T) new Boolean(sp.getBoolean(key, (Boolean) defaultValue));
        } else if (defaultValue instanceof Long) {
            return (T) new Long(sp.getLong(key, (Long) defaultValue));
        } else {
            return null;
        }
    }

    /**
     * 保存值到REFRENCE文件
     *
     * @param key
     * @param value
     */
    public void putPreferences(String key, Object value) {
        final Editor editor = sp.edit();
        if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        }
        editor.commit();
    }
}
